#include <iostream>
#include <memory>
#include "Err.hpp"
#include "TcpServer.hpp"

using namespace ts;
using namespace pr;

Response calculator(const Request& request)
{
    Response tmp;
    switch (request._op)
    {
    case '+':
        tmp._result = request._x + request._y;
        break;
    case '-':
        tmp._result = request._x - request._y;
        break;
    case '*':
        tmp._result = request._x * request._y;
        break;
    case '/':
        if (request._y == 0)
            tmp._code = 1;
        else
            tmp._result = request._x / request._y;
        break;
    case '%':
        if (request._y == 0)
            tmp._code = 2;
        else
            tmp._result = request._x % request._y;
        break;
    default:
        tmp._code = 3;
        break;
    }
    return tmp;
}
void Usage(std::string u)
{
    std::cout << "Usage:\n\t" << u << "serverport" << std::endl;
}
int main(int argv,char* argc[])
{
    if(argv != 2)
    {
        Usage(argc[0]);
        exit(USAGE_ERROR);
    }
    uint16_t port = atoi(argc[1]);
    std::unique_ptr<Server> utsv(new Server(calculator, port));

    // 初始化服务器
    utsv->InitServer();
    // 启动服务器
    utsv->Start();
    return 0;
}